package com.lzm.ydpt.module.order.activity;

import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.byt.framlib.base.BaseActivity;
import com.byt.framlib.baseadapter.lv.LvCommonAdapter;
import com.byt.framlib.baseadapter.lv.LvViewHolder;
import com.byt.framlib.basemvp.boby.FormBodys;
import com.byt.framlib.commonwidget.LoadingTip;
import com.byt.framlib.commonwidget.NoScrollListview;
import com.byt.framlib.commonwidget.NormalTitleBar;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.PriceRules;
import com.lzm.ydpt.entity.RulesFreight;
import com.lzm.ydpt.mvp.contract.PriceRulesContract;
import com.lzm.ydpt.mvp.presenterimpl.PriceRulesPresenterImpl;

import java.util.List;

import butterknife.BindView;

/**
 * 计价规则
 */
public class PriceRulesActivity extends BaseActivity<PriceRulesPresenterImpl> implements PriceRulesContract.View {

    @BindView(R.id.ntb_price_rules)
    NormalTitleBar ntb_price_rules;
    @BindView(R.id.ltip_price_rules)
    LoadingTip ltip_price_rules;

    @BindView(R.id.nolv_basic_freight)
    NoScrollListview nolv_basic_freight;
    @BindView(R.id.nolv_weight_surcharge)
    NoScrollListview nolv_weight_surcharge;
    @BindView(R.id.nolv_special_period)
    NoScrollListview nolv_special_period;

    @Override
    public int getLayoutId() {
        return R.layout.activity_price_rules;
    }

    @Override
    public PriceRulesPresenterImpl initPreData() {
        return new PriceRulesPresenterImpl(this);
    }

    @Override
    public void initView() {
        ntb_price_rules.setTitleText("计价规则");
        ntb_price_rules.setOnBackListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        ltip_price_rules.setLoadingTip(LoadingTip.LoadStatus.loading);
        getPriceRules();
    }

    private void initAdapter(NoScrollListview nolv, List<RulesFreight> mFreightList, int type) {
        LvCommonAdapter<RulesFreight> mLvAdapter = new LvCommonAdapter<RulesFreight>(this, mFreightList, R.layout.item_price_rules_lv) {
            @Override
            protected void convert(LvViewHolder viewHolder, RulesFreight freight, int position) {
                TextView tv_freight_name = viewHolder.getView(R.id.tv_freight_name);
                if (type == 1) {
                    if (freight.getBasics() == 1) {
                        tv_freight_name.setText(Html.fromHtml("<font color=#333333>" + freight.getName() + "    </font>" +
                                "<big><strong><font color= #28AC5F>" + freight.getUnitPrice() + "</font></strong></big><font color=#333333>  元</font>"));
                    } else {
                        tv_freight_name.setText(Html.fromHtml("<font color=#333333>" + freight.getName() + "    每 </font>" +
                                "<big><strong><font color= #28AC5F>" + freight.getUnit() + "</font></strong></big><font color=#333333> 公里加价 </font>" +
                                "<big><strong><font color= #28AC5F>" + freight.getUnitPrice() + "</font></strong></big><font color=#333333> 元</font>"));
                    }
                } else if (type == 2) {
                    if (freight.getBasics() == 1) {
                        tv_freight_name.setText(Html.fromHtml("<font color=#333333>" + freight.getName() + "    </font>" + "<font color=#333333>  不收重量附加费</font>"));
                    } else {
                        tv_freight_name.setText(Html.fromHtml("<font color=#333333>" + freight.getName() + "    每 </font>" +
                                "<big><strong><font color= #28AC5F>" + freight.getUnit() + "</font></strong></big><font color=#333333> 公斤加价 </font>" +
                                "<big><strong><font color= #28AC5F>" + freight.getUnitPrice() + "</font></strong></big><font color=#333333> 元</font>"));
                    }
                } else if (type == 3) {
                    if (freight.getBasics() == 1) {
                        tv_freight_name.setText(Html.fromHtml("<font color=#333333>" + freight.getName() + "    加 </font>" +
                                "<big><strong><font color= #28AC5F>" + freight.getUnitPrice() + "</font></strong></big><font color=#333333>  元</font>"));
                    } else {
                        tv_freight_name.setText(Html.fromHtml("<font color=#333333>" + freight.getName() + "    加 </font>" +
                                "<big><strong><font color= #28AC5F>" + freight.getUnitPrice() + "</font></strong></big><font color=#333333>  元</font>"));
                    }
                }
            }
        };
        nolv.setAdapter(mLvAdapter);
    }

    private void getPriceRules() {
        FormBodys.Builder formBodys = new FormBodys.Builder();
        mPresenter.onPriceRules(formBodys.build());
    }

    @Override
    public void onPriceRules(PriceRules priceRules) {
        if (priceRules != null) {
            ltip_price_rules.setLoadingTip(LoadingTip.LoadStatus.finish);
            initAdapter(nolv_basic_freight, priceRules.getBasicFreight(), 1);
            initAdapter(nolv_weight_surcharge, priceRules.getWeightFreight(), 2);
            initAdapter(nolv_special_period, priceRules.getSpecialPeriodFreight(), 3);
        } else {
            ltip_price_rules.setLoadingTip(LoadingTip.LoadStatus.empty);
        }
    }

    @Override
    public void showMessage(String message, String method) {
        stopProgressDialog();
    }

    @Override
    public void showErrorView(String msg) {
        showErrorView(msg, ltip_price_rules);
        ltip_price_rules.setOnReloadListener(new LoadingTip.onReloadListener() {
            @Override
            public void reload(View view) {
                ltip_price_rules.setLoadingTip(LoadingTip.LoadStatus.loading);
                getPriceRules();
            }
        });
    }
}